package psl;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.text.SimpleDateFormat;
import java.sql.ResultSet;

/**
 * Created by IntelliJ IDEA.
 * User: phiteshkumar
 * Date: Mar 12, 2007
 * Time: 12:08:59 PM
 */
public class SaleEntry extends BasePage
{
    public String msCustomerId;
    public String msSaleNo;
    public String msCardNo;
    public String msCardType;
    public String msTotal;
    public String msTax;
    public String msShping;
    public String msFinal;
    public String msPrice;

    public SaleEntry()
    {
        msViewPage = "SaleEntry.jsp";
    }

    public void setRequest(HashMap fhRequest)
    {
        super.setRequest(fhRequest);
        msCustomerId = (String) mhRequest.get("customerid");
        msSaleNo = (String) mhRequest.get("saleno");
        msCardNo = (String) mhRequest.get("cardno");
        msCardType = (String) mhRequest.get("cardtype");
        msTotal = (String) mhRequest.get("totalamount");
        msShping = (String) mhRequest.get("shippingcost");
        msTax = (String) mhRequest.get("tax");
        msFinal = (String) mhRequest.get("finalamount");
        msPrice = (String) mhRequest.get("productPrice");
    }

    public void performSave()
    {
        String lsError = validate();
        if (lsError != null)
        {
            moUserSession.msError = lsError;
        }
        else
        {
            SimpleDateFormat lodate = new SimpleDateFormat("dd-MMM-yyyy");
            String lsCurDate = lodate.format(new java.util.Date());
            String STATUS = "NDL";
            msSaleNo = CommonServices.getMaxNoFormDB("cust_order_web", "sno", "", "S");
            String lsQuery = "Insert into cust_order_web" +
                             " values (" +
                             "'" + msSaleNo + "'," +
                             "'" + msCustomerId + "'," +
                             "'" + msCardNo + "'," +
                             "'" + msCardType + "'," +
                             msTotal + "," +
                             msShping + "," +
                             msTax + "," +
                             msFinal + "," +
                             "'" + lsCurDate + "'," +
                             "'" + STATUS + "'" +
                             ")";
            System.err.println("cust_order_web:  "+lsQuery);
            moUserSession.msError = DBConn.executeUpdate(lsQuery);
            String lserror = insertInChildTable();
/*
            DeleteTempData();
*/
            if (moUserSession.msError != null)
            {
                moUserSession.msError += lsQuery;
            }
            if (lserror != null)
            {
                moUserSession.msError = lserror;
            }
            else
            {
                moUserSession.msError = null;
                mbSaveFlag = true;
                moUserSession.msConform = "Your transaction is successfully completed. Your order will be delivered within 7 days.";
            }
        }
    }

    public String validate()
    {
        String lsError = null;
        if (msCardNo == null)
        {
            lsError = "Card No is Required";
            return lsError;
        }
        else
        {
            if (CommonServices.isNaN(msCardNo))
            {
                lsError = "Card No is Invalid";
                return lsError;
            }
        }
        if (msCardType == null)
        {
            lsError = "Card Type is Required";
            return lsError;
        }
        return lsError;
    }

    /*
        public void DeleteTempData()
        {
            String lsQuery="Delete from tempdata where cid='"+moUserSession.msCustId+"'";
            DBConn.executeUpdate (lsQuery);
        }
    */
    public String insertInChildTable()
    {
        String lsError = null;
        ArrayList lvSelected = moUserSession.mvSelsected;
        Iterator pid = lvSelected.iterator();
        String lsPid = "";
        int i = 0;
        while (pid.hasNext())
        {
            lsPid = (String) pid.next();
            String lsQuery = "Insert into cust_order_detail_web" +
                             " values (" +
                             "'" + msSaleNo + "'," +
                             "'" + lsPid + "'," +
                             moUserSession.mvQuantity.get(lsPid) + "," +
                             msPrice + "," +
                             msTotal + "," +
                             msFinal +
                             ")";
            String lsQry = "Update product_detail_web Set qty_on_hand=qty_on_hand - " + moUserSession.mvQuantity.get(lsPid) + " Where product_id='" + lsPid + "'";
            lsError = DBConn.executeUpdate(lsQuery);
            lsError = DBConn.executeUpdate(lsQry);
            if (moUserSession.msError != null)
            {
                lsError += lsQuery;
                return lsError;
            }
            i++;
        }
        return lsError;
    }
}
